#include "StdAfx.h"
#include "ChSLogWriter.h"

CChSLogWriter* CChSLogWriter::s_pInstance = NULL;

CChSLogWriter::CChSLogWriter(void)
{
}

CChSLogWriter::~CChSLogWriter(void)
{
}

CChSLogWriter* CChSLogWriter::Instance()
{
	if(NULL == s_pInstance)
		s_pInstance = new CChSLogWriter;
	return s_pInstance;
}

BOOL CChSLogWriter::Initialize(const CString& strFileName)
{
	if( NULL == Instance() )
		return FALSE;

	try
	{
		if(INVALID_HANDLE_VALUE != Instance()->m_file.m_hFile)
			Instance()->m_file.Close();

		Instance()->m_file.Open(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText);
		return TRUE;
	}
	catch (CMemoryException* e)
	{
		e->ReportError();
		return FALSE;	
	}
	catch (CFileException* e)
	{
		e->ReportError();
		return FALSE;
	}
	catch (CException* e)
	{
		e->ReportError();
		return FALSE;
	}
}

void CChSLogWriter::Uninitialize()
{
	if(INVALID_HANDLE_VALUE != Instance()->m_file.m_hFile)
		Instance()->m_file.Close();
}

void CChSLogWriter::WriteLog(const CString& strMsg)
{
	CTime time = CTime::GetCurrentTime();
	CString strMessage = _T("[");
	strMessage += time.Format(_T("%H:%M:%S"));
	strMessage += _T("]");
	strMessage += strMsg;
	m_file.WriteString(strMessage,TRUE);
	m_file.WriteString(_T("\r\n"),TRUE);
}